/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.SuggestAdviceBean;
import com.koron.purpose.service.api.SuggestAdviceService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "suggestAdvice", description = "建言建议")
@RestController
@RequestMapping("/suggestAdvice")
public class SuggestAdviceController {

    @Autowired
    private SuggestAdviceService suggestAdviceService;
    private final static Logger logger=LoggerFactory.getLogger(SuggestAdviceController.class);

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestCompanyName", value = "建言单位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestStatus", value = "建言状态",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTimeStart", value = "建言时间开始(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTimeStop", value = "建言时间结束(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore String suggestCompanyName, @ApiIgnore String suggestStatus,
                                    @ApiIgnore String suggestTimeStart, @ApiIgnore String suggestTimeStop,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return suggestAdviceService.queryForList(corpCode, suggestCompanyName, suggestStatus,
                suggestTimeStart, suggestTimeStop,
                page, pageSize);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryById.htm", method = RequestMethod.GET)
    public MessageBean queryById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return suggestAdviceService.queryById(id);
    }

    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteSuggestAdviceById.htm", method = RequestMethod.POST)
    @Log(title = "根据ID删除建言建议", businessModule = "安全文化", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean deleteSuggestAdviceById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return suggestAdviceService.deleteSuggestAdviceById(id);
    }

    @ApiOperation("新增建言建议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTitle", value = "建言标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestContent", value = "建言内容", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestPerson", value = "建言人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestPersonName", value = "建言人姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestCompany", value = "建言单位", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestCompanyName", value = "建言单位名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestStatus", value = "建言状态(01-草稿、02-审批中、03-已采纳、04-未采纳)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "giveMark", value = "评分",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "personCode", value = "审批人code", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/insertSuggestAdvice.htm", method = RequestMethod.POST)
    @Log(title = "新增建言建议", businessModule = "安全文化", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean insertSuggestAdvice(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           @ApiIgnore SuggestAdviceBean bean, @ApiIgnore String personCode) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return suggestAdviceService.insertSuggestAdvice(user, bean, personCode);
    }

    @ApiOperation("修改建言建议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTitle", value = "建言标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestContent", value = "建言内容", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acceptCompany", value = "采纳单位",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "acceptCompanyName", value = "采纳单位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestStatus", value = "建言状态(01-草稿、02-审批中、03-已采纳、04-未采纳)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "giveMark", value = "评分",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "personCode", value = "审批人code", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/updateSuggestAdvice.htm", method = RequestMethod.POST)
    @Log(title = "修改建言建议", businessModule = "安全文化", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean updateSuggestAdvice(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           @ApiIgnore SuggestAdviceBean bean, @ApiIgnore String personCode) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return suggestAdviceService.updateSuggestAdvice(user, bean, personCode);
    }


    @ApiOperation("建言建议列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestCompanyName", value = "建言单位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestStatus", value = "建言状态",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTimeStart", value = "建言时间开始(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "suggestTimeStop", value = "建言时间结束(yyyy-MM-dd)",  paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore String suggestCompanyName, @ApiIgnore String suggestStatus,
                                    @ApiIgnore String suggestTimeStart, @ApiIgnore String suggestTimeStop) {
        if (null == user) {
            return null;
        }
        List<Map> list = suggestAdviceService.exportList(user, corpCode, suggestCompanyName,suggestStatus,suggestTimeStart,suggestTimeStop);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/suggestAdviceList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/suggestAdviceList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("建言建议列表", targetFile, "suggestAdviceList.btl", map);
    }
}
